//
//  LZSegmentView.h
//  FastCall
//
//  Created by Carlson Lee on 2017/12/8.
//  Copyright © 2017年 XiaoNiu Ltd. All rights reserved.
//

#import "FCSegmentManager.h"

@interface FCSegmentManager ()<FCSegmentViewDelegate, UIScrollViewDelegate>
{
    BOOL _isContain;
}
@property (nonatomic, strong) FCSegmentView* segmentTagView;

@property (nonatomic, strong) UIScrollView* scroll;

@property (nonatomic, strong) NSArray* vcs;

@end

@implementation FCSegmentManager

- (instancetype)initWithDelegate:(id<FCSegmentManagerDelegate>)delegate
{
    if(self = [super init]){
        _index = 0;
        _delegate = delegate;
    }
    return self;
}

- (void)configSegmentView:(FCSegmentView* )segment
{
    self.segmentTagView = (FCSegmentView* )segment;
    if(_delegate && [_delegate respondsToSelector:@selector(titlesOfSegmentManager)]){
        NSArray* titles = [_delegate titlesOfSegmentManager];

        if(segment){
            if(!self.segmentTagView.superview){
                _isContain = YES;
                [self insertSubview:self.segmentTagView aboveSubview:self.scroll];
            }
            segment.delegate = self;
            [self.segmentTagView setTags:titles atIndex:_index];
        }
    }
    
    if(_delegate && [_delegate respondsToSelector:@selector(vcsOfSegmentManager)]){
        self.vcs = [_delegate vcsOfSegmentManager];
        [self.vcs enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIViewController* vc = obj;
            [self.scroll addSubview:vc.view];
        }];
    }
    
    [self segmentSelectWithTag:_index];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat f_y = 0;
    if(_isContain){
        f_y = CGRectGetMaxY(self.segmentTagView.frame);
    }
    self.scroll.frame = CGRectMake(0, f_y, kBounds.width, kBounds.height-f_y);
    __block CGFloat f_x = 0;
    [self.vcs enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIViewController* vc = obj;
        vc.view.frame = CGRectMake(f_x, 0, kBounds.width, kBounds.height-f_y);
        f_x += kScreen.width;
    }];
    [self.scroll setContentSize:CGSizeMake(kBounds.width*self.vcs.count, 0)];
}

#pragma mark----LZSegmentTagsViewDelegate

- (void)segmentSelectWithTag:(NSInteger)index
{
    CGPoint offset = self.scroll.contentOffset;
    offset.x = kBounds.width*index;
    [self.scroll setContentOffset:offset];
    
    if(_delegate && [_delegate respondsToSelector:@selector(segmentSelectAtIndex:)]){
        [_delegate segmentSelectAtIndex:index];
    }
}

#pragma mark----UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetx = scrollView.contentOffset.x;
    [self.segmentTagView segmentScrollContentOffset:offsetx];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
}

- (UIScrollView *)scroll
{
    if(!_scroll){
        _scroll = [[UIScrollView alloc]init];
        [self addSubview:_scroll];
        _scroll.delegate = self;
        [_scroll setPagingEnabled:YES];
        [_scroll setBackgroundColor:ColorFromRGB(0xf0f0f0)];
        [_scroll setShowsHorizontalScrollIndicator:NO];
        if(@available(ios 11.0, *)){
            _scroll.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _scroll;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
